package com.example.backend.mapper;

import com.example.backend.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public interface UserMapper {
    @Insert("insert into user(id,yhm,pwd,yhtx,sjh,age,sex) values(#{id},#{yhm},#{pwd},#{yhtx},#{sjh},#{age},#{sex})")
    int addUser(User user);

    @Update("update user set yhm = #{yhm}, yhtx = #{yhtx}, pwd = #{pwd}, sjh = #{sjh}, age = #{age}, sex = #{sex} where id = #{id}")
    int updateUser(User user);


    @Delete("delete from user where id=#{id}")
    int deleteUserById(Serializable id);

    @Select("select id,yhm,pwd,yhtx,sjh,age,sex from user where id=#{id}")
    User selectUserById(Serializable id);

    @Select("select id,yhm,pwd,yhtx,sjh,age,sex from user where sjh=#{sjh} and pwd = #{pwd}")
    User selectUser(User user);

    @Select("select id,yhm,pwd,yhtx,sjh,age,sex from user")
    ArrayList<User> selectAll(User user);

    @SelectProvider(type = UserMapperProvider.class, method = "getSelectUsersSql")
    ArrayList<User> selectUsers(User user);

    @Select("select id,yhm,pwd,yhtx,sjh,age,sex from user where sjh=#{sjh}")
    ArrayList<User> register(User user);

    @Select("select count(*) from user")
    int countUsers();
    
    // 获取用户年龄分布数据
    @Select("select case when age < 18 then '未成年' when age between 18 and 30 then '18-30岁' when age between 31 and 50 then '31-50岁' else '50岁以上' end as ageGroup, count(*) as count from user group by ageGroup")
    List<Map<String, Object>> getUserAgeDistribution();




}
